#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Filename      :json_demo.py
@Description   :
@Datatime      :2020/09/06 16:16:41
@Author        :AshJo
@Version       :v1.0
'''

import json


# l1 = [1, 2, 3]
# d1 = {'name':'Tony','age':18,'working':True}
# t1 = ('apple','orange','grape')
# 将指定的对象转换成json格式的字符串
# s = json.dumps(l1)
# print(s, type(s))


# 将json内容写入到文件
# with open(r'day16\b.txt',encoding='utf-8',mode='at') as f:
#     json.dump(s,f)


# 从文件中反序列化
# with open(r'day16\b.txt',encoding='utf-8') as f:
#     res = json.load(f)
#     print(res)

# json.dumps(obj)
# json.dump(obj,f)
# json.loads(s)
# json.load(f)

# day17 补充部分

# dic = {'username': '喜羊羊', 'password': 123, 'status': False}
# # 特殊的字符串
# st = json.dumps(dic, ensure_ascii=False)
# print(st, type(st))
#
# # 反转回去
# dic1 = json.loads(st)
# print(dic1, type(dic1))


# 一次序列化写入和读取的正确写法

d1 = {'username': '喜羊羊', 'password': 123, 'status': False}
d2 = {'username': '美羊羊', 'password': 456, 'status': False}
d3 = {'username': '懒羊羊', 'password': 789, 'status': False}

# with open('json1.txt', encoding='utf-8', mode='w') as f:
#     f.write(json.dumps(d1, ensure_ascii=False) + '\n')
#     f.write(json.dumps(d2, ensure_ascii=False) + '\n')
#     f.write(json.dumps(d3, ensure_ascii=False) + '\n')

# with open('json1.txt', encoding='utf-8', mode='r') as f:
#     for i in f:
#         st = json.loads(i)
#         print(st, type(st))




